package gateway.route;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.List;

public class PrometheusDataParser {

    public static void main(String[] args) {
        // 示例数据，模拟从Prometheus API获取的数据行
        String prometheusDataLine = "{\"metric\": {\"__name__\": \"http_requests_total\", \"method\": \"GET\", \"handler\": \"/api/data\"}, \"values\": [[1640880000.0, \"10\"], [1640880060.0, \"15\"]]}";

        // 正则表达式用于匹配时间戳和值
        Pattern pattern = Pattern.compile("\"values\": \\[\\[(\\d+\\.\\d+),\\s+\"(\\d+)\"\\]");

        // 创建Matcher对象
        Matcher matcher = pattern.matcher(prometheusDataLine);

        // 存储解析结果
        List<Pair<Double, Integer>> parsedData = new ArrayList<>();

        // 查找匹配项并处理
        while (matcher.find()) {
            double timestamp = Double.parseDouble(matcher.group(1)); // 时间戳
            int value = Integer.parseInt(matcher.group(2)); // 值
            parsedData.add(new Pair<>(timestamp, value));
        }

        // 打印解析结果
        for (Pair<Double, Integer> dataPoint : parsedData) {
            System.out.println("Timestamp: " + dataPoint.getKey() + ", Value: " + dataPoint.getValue());
        }
    }

    // 辅助类用于存储时间戳-值对
    public static class Pair<K, V> {
        private K key;
        private V value;

        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }
    }
}
